package com.company.project.search.engine;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;

public class Param implements Cloneable {

    public static final String TYPE_CJFQ = "1"; // 学术期刊
    public static final String TYPE_CDMD = "2"; // 学位论文
    public static final String TYPE_CIPD = "3"; // 会议
    public static final String TYPE_CCND = "4"; // 报纸
    public static final String TYPE_BDZK = "5"; // 图书
    public static final String TYPE_CISD = "6"; // 标准规定
    public static final String TYPE_SNAD = "7"; // 成果
    public static final String TYPE_CCJD = "8"; // 学术辑刊
    public static final String TYPE_GXDB_SECTION = "9"; // 古籍
    public static final String TYPE_CJFN = "10"; // 特色期刊
    public static final String TYPE_CCVD = "11"; // 视频
    public static final String TYPE_CYFD = "12"; // 年鉴
    public static final String TYPE_SCOD = "13"; // 专利
    public static final String TYPE_LAYER = "14";// 法律

    public String token;
    public String keyword;
    public int curPage;
    public int engine;
    public String[] category;
    public long duration;

    /*E1-Param*/
    private String mE1DBCode;
    private String mE1KuaKuCodes;

    public Param(String _token) {
        this.keyword = new String(Base64.getDecoder().decode(_token), StandardCharsets.UTF_8);
//        if (keyword.contains("@")) {
//            this.keyword = keyword.split("@")[0];
//        }
        System.out.println(keyword);
        this.token = _token;
        this.mE1DBCode = "SCDB";
        this.mE1KuaKuCodes = "CJFQ,CDMD,CIPD,CCND,CISD,SNAD,BDZK,CCJD,CCVD,CJFN";
        this.duration = System.currentTimeMillis();
    }

    public Param setCurPage(Integer page) {
        this.curPage = page;
        return this;
    }

    public Param setCategory(String _category) {
        if (_category == null) {
            this.mE1DBCode = "SCDB";
            return this;
        }
        this.token = this.token.concat(_category.replaceAll("[\\D|,]", ""));
        System.out.println(this.token);

        String[] array = _category.split("[c|C]");

        if (array.length == 1) {
            switch (array[0]) {
                case TYPE_CJFQ:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CJFQ";
                    break;
                case TYPE_CDMD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CDMD";
                    break;
                case TYPE_CIPD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CIPD";
                    break;
                case TYPE_CCND:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CCND";
                    break;
                case TYPE_BDZK:
                    this.mE1KuaKuCodes = this.mE1DBCode = "BDZK";
                    break;
                case TYPE_CISD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CISD";
                    break;
                case TYPE_SNAD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "SNAD";
                    break;
                case TYPE_CCJD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CCJD";
                    break;
                case TYPE_GXDB_SECTION:
                    this.mE1KuaKuCodes = this.mE1DBCode = "TYPE_GXDB_SECTION";
                    break;
                case TYPE_CJFN:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CJFN";
                    break;
                case TYPE_CCVD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CCVD";
                    break;
                case TYPE_CYFD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "CYFD";
                    break;
                case TYPE_SCOD:
                    this.mE1KuaKuCodes = this.mE1DBCode = "SCOD";
                    break;
                default:
                    this.mE1DBCode = "SCDB";
                    this.mE1KuaKuCodes = "CJFQ,CDMD,CIPD,CCND,CISD,SNAD,BDZK,CCJD,CCVD,CJFN";
            }
        } else {
            this.mE1DBCode = "SCDB";
            StringBuilder sb = new StringBuilder(128);
            for (String value : array) {
                sb.append(appendKuaKuCode(value));
            }
            this.mE1KuaKuCodes = sb.delete(sb.length() - 1, sb.length()).toString();
        }
        return this;
    }

    public String getMapId() {
        return this.token + this.curPage;
    }

    private String appendKuaKuCode(String type) {
        switch (type) {
            case TYPE_CJFQ:
                return "CJFQ,";
            case TYPE_CDMD:
                return "CDMD,";
            case TYPE_CIPD:
                return "CIPD,";
            case TYPE_CCND:
                return "CCND,";
            case TYPE_BDZK:
                return "BDZK,";
            case TYPE_CISD:
                return "CISD,";
            case TYPE_SNAD:
                return "SNAD,";
            case TYPE_CCJD:
                return "CCJD,";
            case TYPE_GXDB_SECTION:
                return "TYPE_GXDB_SECTION,";
            case TYPE_CJFN:
                return "CJFN,";
            case TYPE_CCVD:
                return "CCVD,";
            case TYPE_CYFD:
                return "CYFD,";
            case TYPE_SCOD:
                return "SCOD,";
            default:
                return "";
        }
    }

    public String getE1DBCode() {
        return mE1DBCode;
    }

    public String getE1KuaKuCodes() {
        return mE1KuaKuCodes;
    }

    @Override
    public String toString() {
        return "Param{" +
                "requestId='" + token + '\'' +
                ", keyword='" + keyword + '\'' +
                ", curPage=" + curPage +
                ", engine=" + engine +
                ", types=" + Arrays.toString(category) +
                ", mE1DBCode='" + mE1DBCode + '\'' +
                ", mE1KuaKuCodes='" + mE1KuaKuCodes + '\'' +
                '}';
    }


    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
